﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ECI.CommonLibrary;
using System.Data;
using System.Web;
using ECI.Entities;
using ECI.Controls;
using ECI.CoreLibrary;
using ECI.WebLib.Data;
using ECI.DynamicData.DataCache;

namespace ECI.DynamicData.ElementBuilder
{
    public class TChoose : IElement
    {
        private ECIChoose txtBox = null;
        private bool IsMulti;

        public override void Create(ElementParameter param)
        {
            this.Init(param);

            this.CreateContainer();

            txtBox = new ECIChoose();
            txtBox.ECIType = param.Field.DictionaryTypeConfig;
            txtBox.Attributes.Add("sql", param.Field.SqlAttribute);
            txtBox.Attributes.Add("mainalias", param.Field.MainAlias);
            txtBox.Attributes.Add("maxlength", Field.FieldLength.ToString());

            //2010-06-29 增加控件宽度属性控制 控件的显示宽度
            if (param.type == "Edit")
            {
                if (param.Field.ControlWidth.Length > 0)
                {
                    txtBox.Style["width"] = param.Field.ControlWidth;
                }
                else
                {
                    txtBox.Style["width"] = "85%";//占整个单元格的85%
                }
            }
            else
            {
                txtBox.Style["width"] = "85%";//占整个单元格的85%
            }


            txtBox.ID = "txt" + param.Field.Name;
            if (param.EditMode == "BatchUpdate") txtBox.ID = "txtBatch" + param.Field.Name;
            txtBox.ECIType = param.Field.DictionaryTypeConfig;
            if (param.Field.DropdownlistConfig.Length > 0)
            {
                txtBox.Attributes.Add("MULTI", "MULTI");
            }

            txtBox.AutoCompleteType = AutoCompleteType.Disabled;
            this.IsMulti = Field.DropdownlistConfig.Contains("MULTI");

            if (this.IsMulti)
            {
                txtBox.MultiSelect = true;
                txtBox.AutoHide = true;
            }

            if (param.type == "Edit" && DynamicQuery.EditHideForSystemField(param.Field))
            {
                param.HideCell.Controls.Add(txtBox);
            }
            else if (param.Field.Hidden == "Y")
            {
                param.HideCell.Controls.Add(txtBox);
            }
            else
            {
                if (Field.AfterElement != "")
                {
                    TableCell cell = this.GetControlCell(param.TableRow.Parent as Table, Field.AfterElement.Trim());
                    if (cell == null)
                        throw new Exception(Field.MainAlias + ": 没找到上级元素" + Field.AfterElement);
                    Label lab = new Label();
                    lab.Text = Field.Udf2;
                    cell.Controls.Add(lab);
                    cell.Controls.Add(txtBox);
                }
                else
                {
                    MyTableCell.Controls.Add(txtBox);
                    param.TableRow.Cells.Add(MyTableCell);
                }
            }

            if (param.DataRow != null)
            {

                SetChooseText(param.DataRow[param.Field.Name].ToString());

            }

            #region 复制新增时 是否清除控件内容
            if (param.Field.CopyRemoveData.ToMyString() == "1" && param.EditMode.ToLower() == "copy")
            {
                SetChooseText("");
            }
            #endregion
        }


        private void SetChooseText(string value)
        {
            txtBox.Text = value;

            if (this.IsMulti)
            {
                txtBox.Text = "……";
                txtBox.MultiValue = value;
                txtBox.GetNameByValue();
            }

        }


        #region IElement 成员


        public override void Locked()
        {
            DataRow dataRow = param.DataRow;
            string editMode = param.EditMode;

            if (Field.EditLockMode == "LOCKFULL") LockControl();
            else if (dataRow != null && editMode.ToLower() != "copy" && Field.EditLockMode == "LOCKEDIT") LockControl();
            else if (dataRow == null && Field.EditLockMode == "LOCKADD") LockControl();

        }

        public override void Default()
        {
            DataRow dataRow = param.DataRow;
            if (Field.DefaultValue != "" && dataRow == null)
            {
                txtBox.Text = Field.DefaultValue;
            }
        }

        public override void SystemField()
        {
            DataRow dataRow = param.DataRow;
            string editMode = param.EditMode;
            txtBox.ReadOnly = true;
            txtBox.BackColor = System.Drawing.Color.Gainsboro;
            if (txtBox is ECIChoose) ((ECIChoose)txtBox).IsGeneric = true;

            if (Field.SystemData.ToMyString() != "" && Field.SystemDataTrigger.ToMyString() == "INSERT" && Field.SystemDataShow.ToMyString() == "1" && dataRow == null)
            {
                txtBox.Text = GetSystemDataValueForChoose();
            }
        }

        public override void Cascade()
        {
            DataRow dataRow = param.DataRow;
            if (dataRow == null)
            {
                string value = PageHelper.Request(Field.Name);
                if (value != "")
                {
                    txtBox.Text = value + "|" + GetName(param.Field.DictionaryTypeConfig, value);
                }
            }
        }

        #endregion

        private void LockControl()
        {
            txtBox.ReadOnly = true;
            txtBox.BackColor = System.Drawing.Color.Gainsboro;
            if (txtBox.CssClass == "Wdate") txtBox.Attributes.Remove("onclick");
            if (txtBox is ECIChoose) ((ECIChoose)txtBox).IsGeneric = true;
        }


        public override string GetValue()
        {
            string value = "";
            if (EditMode == DynamicEditMode.BatchUpdate)
            {
                CheckBox chk = param.Table.FindControl("chk" + Field.Name) as CheckBox;
                if (chk.Checked)
                {
                    ECIChoose txtBox = param.Table.FindControl("txtBatch" + Field.Name) as ECIChoose;
                    value = txtBox.Code;

                    return DynamicQuery.GetDynamicSQL(Field, value, DynamicEditMode.Update);

                }
                else
                {
                    return "";
                }

            }
            else
            {

                if (param.Field.SystemData.ToMyString() == "")
                {
                    ECIChoose txtBox = param.Table.FindControl("txt" + Field.Name) as ECIChoose;
                    value = txtBox.Code;
                    if (Field.DropdownlistConfig.Contains("MULTI"))
                    {
                        value = txtBox.MultiValue;
                    }
                }
                else
                {
                    value = this.GetSystemDataValue();
                }

                return DynamicQuery.GetDynamicSQL(Field, value, EditMode);
            }


        }

        public override void DataKeep()
        {
            if (PageHelper.CurrentPage.IsPostBack)
            {
                if (param.Field.DataKeep == "Y")
                {
                    string value = PageHelper.Request("txt" + param.Field.Name);
                    SetChooseText(value);
                }
            }
        }

        public override void QueryDefault()
        {
            if (Field.Udf6 == "Y")
                LockControl();
        }

        public override void ClientEventRegister()
        {

        }

        public override string GetRealValue()
        {
            throw new NotImplementedException();
        }


        private string GetName(string type, string value)
        {
            ECI.CoreLibrary.CommonLibrary cmn = new ECI.CoreLibrary.CommonLibrary();

            EciDataHelp dataHelper = new EciDataHelp();
            dataHelper.StartSetSQLValue();
            dataHelper.QueryType = type;
            dataHelper.DataType = "CHOOSE";
            dataHelper = dataHelper.FetchEntity();
            string sql = dataHelper.ConvertSql + " AND " + dataHelper.DataChangeCode + " = '" + value + "'";
            DataTable dt = DBHelper.GetDataTable(sql);
            if (!cmn.CheckEOF(dt))
            {
                return "";
            }

            return dt.Rows[0][dataHelper.DataChangeName].ToString();
        }
    }
}
